<script>
function registerPupil($item) {
	$item.fadeOut(function() {
		var $list = $("ol", $("#entries")).length ? $("ol", $("#entries")) : $("<ol class='ui-state-default'/>").appendTo($entriesdata);
		$item.appendTo($list).fadeIn();
	});
	$item.unbind('click').click(function() {
		recyclePupil($(this));
	});
	var selectedroom = $('#selectedroom').val();
	var date = $("#date").val();
	$.getJSON('/~Marc/kivaou/ajax/set_entry', {
		'room': selectedroom,
		'pupil_id': $item.attr('pupil_id'),
		'date': date
	}, function(result) {
		console.log(result);
	});
}

function recyclePupil($item) {
	var selectedclass = $('#selectedclass').val();
	var pupilclass = $item.attr('pupil_class');
	if (selectedclass == pupilclass) {
		$("#pupilname").html($item.html());
		$("#entrydate").html($("#date").val());
		$("#cancel").click(function() {
			$("#modalConfirm").modal('hide');
		});
		$("#confirm").click(function() {
			$item.fadeOut(function() {
				if (selectedclass == pupilclass) {
					$item.appendTo($("#pupilsdata")).fadeIn();
					$item.unbind('click').click(function() {
						registerPupil($(this));
					});
				}
			});
			var selectedroom = $('#selectedroom').val();
			var date = $("#date").val();
			$.getJSON('/~Marc/kivaou/ajax/delete_entry', {
				'room': selectedroom,
				'pupil_id': $item.attr('pupil_id'),
				'date': date
			}, function(result) {
				console.log(result);
			});
			$("#modalConfirm").modal('hide');
		});
		$("#modalConfirm").modal('show');
	}else{
		$("#ok").click(function() {
			$("#modalUnauthorised").modal('hide');
		});		
		$("#modalUnauthorised").modal('show');
	}
}

function getEntries() {
	var selectedroom = $('#selectedroom').val();
	var date = $("#date").val();
	$.getJSON('/~Marc/kivaou/ajax/get_entries', {
		'room': selectedroom,
		'date': date
	}, function(result) {
		var contentHtml = "";
		$(result).each(

		function(index, item) {
			if (item.pupilclass == $('#selectedclass').val()) {
				contentHtml += "<li pupil_class=\"" + item.pupilclass + "\" pupil_id=\"" + item.id + "\" class=\"ui-state-default\">";
			} else {
				contentHtml += "<li pupil_class=\"" + item.pupilclass + "\" pupil_id=\"" + item.id + "\" class=\"ui-state-disabled\">";
			}
			contentHtml += item.surname;
			contentHtml += " ";
			contentHtml += item.name;
			contentHtml += " ( ";
			contentHtml += item.pupilclass;
			contentHtml += " )";
			contentHtml += "</li>";
		});
		$("#entriesdata").html(contentHtml);
		var $entriesdata = $("#entriesdata");
		$("li", $entriesdata).click(function() {
			recyclePupil($(this));
		});
	});
}

function getUnregisteredPupils() {
	var selectedclass = $('#selectedclass').val();
	var date = $("#date").val();
	$.getJSON('/~Marc/kivaou/ajax/get_unregistered_pupils', {
		'class': selectedclass,
		'date': date
	}, function(result) {
		var contentHtml = "";
		$(result).each(

		function(index, item) {
			contentHtml += "<li pupil_class=\"" + selectedclass + "\" pupil_id=\"" + item.id + "\" class=\"ui-state-default\">";
			contentHtml += item.surname;
			contentHtml += " ";
			contentHtml += item.name;
			contentHtml += " ( ";
			contentHtml += selectedclass;
			contentHtml += " )";
			contentHtml += "</li>";
		});
		$('#pupilsdata').html(contentHtml);
		var $pupilsdata = $("#pupilsdata");
		$("li", $pupilsdata).click(function() {
			registerPupil($(this));
		});
		// update the style of the selected class entries
		var $entriesdata = $("#entriesdata");
		$("li", $entriesdata).each(function( index ) {
			if($(this).attr("pupil_class")==selectedclass){
				$(this).removeClass().addClass('ui-state-default');
			}else{
				$(this).removeClass().addClass('ui-state-disabled');
			}
		});
	});
}
$("#date").datepicker({
	beforeShowDay: $.datepicker.noWeekends
}).on('changeDate', function(ev) {
	getUnregisteredPupils();
	getEntries();
})
$('#selectedroom').change(function() {
	getEntries();
});
$('#selectedclass').change(function() {
	getUnregisteredPupils();
}); 
</script>